home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / SciAn / src / ScianRepobjFunctions.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  10KB  |  384 lines

  1. /*ScianRepobjFunctions.c
  2.   Eric Pepke
  3.   10 August 1993
  4.  
  5.   Does repobj functions, i.e., functions that work on individual windows'
  6.   REPOBJ variables.
  7. */
  8.  
  9. #include "Scian.h"
  10. #include "ScianTypes.h"
  11. #include "ScianLists.h"
  12. #include "ScianWindows.h"
  13. #include "ScianColors.h"
  14. #include "ScianIDs.h"
  15. #include "ScianObjWindows.h"
  16. #include "ScianButtons.h"
  17. #include "ScianErrors.h"
  18. #include "ScianDraw.h"
  19. #include "ScianControls.h"
  20. #include "ScianArrays.h"
  21. #include "ScianScripts.h"
  22. #include "ScianVisWindows.h"
  23. #include "ScianIcons.h"
  24. #include "ScianEvents.h"
  25. #include "ScianStyle.h"
  26. #include "ScianRepobjFunctions.h"
  27. #include "ScianHelp.h"
  28. #include "ScianFilters.h"
  29. #include "ScianFileSystem.h"
  30. #include "ScianSockets.h"
  31. #include "ScianPointers.h"
  32. #include "ScianMenus.h"
  33. #include "ScianPreferences.h"
  34. #include "ScianFileSystem.h"
  35. #include "ScianAnimation.h"
  36. #include "ScianSymbols.h"
  37.  
  38. ObjPtr repobjActionClass;
  39.  
  40. typedef struct
  41.     {
  42.     char *name;        /*Name of function*/
  43.     NameTyp message;    /*Message to send to repobj*/
  44.     char *buttonHelp;    /*Button help*/
  45.     char *menuHelp;        /*Menu help*/
  46.     char *scriptLine;    /*Line for script*/
  47.     ObjPtr menu;        /*Which menu this routine is in*/
  48.     int menuGroup;        /*Which menu group this function is in*/
  49.     } RepobjFunction;
  50.  
  51. int nRepobjFunctions = 0;    /*# of repobj functions*/
  52.  
  53. static RepobjFunction *repobjFunctions;
  54.  
  55. #ifdef PROTO
  56. int NameToRepobjFunction(char *name)
  57. #else
  58. int NameToRepobjFunction(name)
  59. char *name;
  60. #endif
  61. /*Converts a name to a function number, or -1*/
  62. {
  63.     int funcNum;
  64.  
  65.     /*Search for the named function*/
  66.     for (funcNum = 0; funcNum < nRepobjFunctions; ++funcNum)
  67.     {
  68.     if (0 == strcmp2(name, repobjFunctions[funcNum] . name))
  69.     {
  70.         break;
  71.     }
  72.     }
  73.  
  74.     if (funcNum >= nRepobjFunctions)
  75.     {
  76.     /*Not found, too bad.*/
  77.     char nameStr[300];
  78.     sprintf(nameStr, "Internal error: name '%s' not found", name);
  79.     ReportError("NameToRepobjFunction", nameStr);
  80.     return -1;
  81.     }
  82.     return funcNum;
  83. }
  84.  
  85. #ifdef PROTO
  86. Bool RepobjFunctionScriptLine(char *line)
  87. #else
  88. Bool RepobjFunctionScriptLine(line)
  89. char *line;
  90. #endif
  91. /*If s is a script line, finds out if it is a valid script line for a
  92.   repobj function.  If so, does it and returns true, otherwise does nothing
  93.   and returns false.  Determines whether it is valid by matching the 
  94.   script line*/
  95. {
  96.     int funcNum;
  97.     register char *s, *p, *t;
  98.  
  99.     /*For all possible functions*/
  100.     for (funcNum = 0; funcNum < nRepobjFunctions; ++funcNum)
  101.     {
  102.     /*Try to match script line against script*/
  103.     s = line;
  104.     p = repobjFunctions[funcNum] . scriptLine;
  105.  
  106.     if (!p) continue;
  107.  
  108.     SKIPBLANKS(s);
  109.     SKIPBLANKS(p);
  110.  
  111.     while (*p)
  112.     {
  113.         if (isspace(*p))
  114.         {
  115.         SKIPBLANKS(s);
  116.         SKIPBLANKS(p);
  117.         }
  118.         else if (toupper(*p) != toupper(*s))
  119.         {
  120.         /*Failure to match*/
  121.         break;
  122.         }
  123.         else
  124.         {
  125.         ++s;
  126.         ++p;
  127.         }
  128.     }
  129.  
  130.     if (*p == 0)
  131.     {
  132.         ObjPtr list;
  133.         ThingListPtr runner;
  134.         ObjPtr object;
  135.         /*It's a match!  Do this function and return true*/
  136.  
  137.         Log(repobjFunctions[funcNum] . scriptLine);
  138.         Log("\n");
  139.  
  140.         object = GetVar((ObjPtr) selWinInfo, REPOBJ);
  141.         if (object)
  142.         {
  143.         DeferMessage(object, repobjFunctions[funcNum] . message);
  144.         }
  145.  
  146.         return true;
  147.     }
  148.     }
  149.     return false;
  150. }
  151.  
  152. #ifdef PROTO
  153. void DefineRepobjFunction(char *funcName, NameTyp message,
  154.     char *scriptLine,
  155.     char *buttonHelp,
  156.     char *menuHelp, ObjPtr menu, int menuGroup)
  157. #else
  158. void DefineRepobjFunction(funcName, message, scriptLine,
  159.     buttonHelp, menuHelp, menu, menuGroup)
  160. char *funcName;
  161. NameTyp message;
  162. char *scriptLine;
  163. char *buttonHelp;
  164. char *menuHelp;
  165. ObjPtr menu;
  166. int menuGroup;
  167. #endif
  168. /*Defines a repobj function
  169.     funcName        is the name of the function for the menu and buttons
  170.     message        is a message to do it
  171.     scriptLine        is the script line
  172.     buttonHelp        is the help string for a button, or nothing for no button
  173.     menuHelp        is the help string for a menu
  174.     menu        is which menu to use, or NULLOBJ for none
  175.     menuGroup        is the menu group
  176. */
  177. {
  178.     if (nRepobjFunctions)
  179.     {
  180.     repobjFunctions = Realloc(repobjFunctions, (nRepobjFunctions + 1) * sizeof(RepobjFunction));
  181.     }
  182.     else
  183.     {
  184.     repobjFunctions = Alloc((nRepobjFunctions + 1) * sizeof(RepobjFunction));
  185.     }
  186.  
  187.     if (funcName)
  188.     {
  189.     repobjFunctions[nRepobjFunctions] . name = Alloc(strlen(funcName) + 1);
  190.     strcpy(repobjFunctions[nRepobjFunctions] . name, funcName);
  191.     }
  192.     else
  193.     {
  194.     repobjFunctions[nRepobjFunctions] . name = (char *) 0;
  195.     }
  196.     repobjFunctions[nRepobjFunctions] . message = message;
  197.     repobjFunctions[nRepobjFunctions] . menu = menu;
  198.     repobjFunctions[nRepobjFunctions] . menuGroup = menuGroup;
  199.     if (scriptLine)
  200.     {
  201.     repobjFunctions[nRepobjFunctions] . scriptLine = Alloc(strlen(scriptLine) + 1);
  202.     strcpy(repobjFunctions[nRepobjFunctions] . scriptLine, scriptLine);
  203.     }
  204.     else
  205.     {
  206.     repobjFunctions[nRepobjFunctions] . scriptLine = (char *) 0;
  207.     }
  208.     if (buttonHelp)
  209.     {
  210.     repobjFunctions[nRepobjFunctions] . buttonHelp = Alloc(strlen(buttonHelp) + 1);
  211.     strcpy(repobjFunctions[nRepobjFunctions] . buttonHelp, buttonHelp);
  212.     }
  213.     else
  214.     {
  215.     repobjFunctions[nRepobjFunctions] . buttonHelp = (char *) 0;
  216.     }
  217.     if (menuHelp)
  218.     {
  219.     repobjFunctions[nRepobjFunctions] . menuHelp = Alloc(strlen(menuHelp) + 1);
  220.     strcpy(repobjFunctions[nRepobjFunctions] . menuHelp, menuHelp);
  221.     }
  222.     else
  223.     {
  224.     repobjFunctions[nRepobjFunctions] . menuHelp = (char *) 0;
  225.     }
  226.  
  227.     /*Put it on the appropriate menu*/
  228.     if (menu)
  229.     {
  230.     /*Put it on menu*/
  231.     ObjPtr action;
  232.  
  233.     action = NewAction(funcName, repobjActionClass);
  234.     SetVar(action, HELPSTRING, NewString(menuHelp));
  235.     AddMenuItem(menu, action, menuGroup);
  236.     }
  237.  
  238.     ++nRepobjFunctions;
  239. }
  240.  
  241. ObjPtr RepobjAction(action)
  242. ObjPtr action;
  243. /*Does the action method of a repobj action*/
  244. {
  245.     int whichFunction;
  246.     ObjPtr var;
  247.  
  248.     var = GetVar(action, NAME);
  249.     if (var)
  250.     {
  251.     whichFunction = NameToRepobjFunction(GetString(var));
  252.     if (contextHelp)
  253.     {
  254.         /*Give help on the action*/
  255.         ContextHelp(action);
  256.         contextHelp = false;
  257.         MySetCursor(0);
  258.     }
  259.     else
  260.     {
  261.         /*Log it*/
  262.         if (repobjFunctions[whichFunction] . scriptLine)
  263.         {
  264.         Log(repobjFunctions[whichFunction] . scriptLine);
  265.         Log("\n");
  266.         }
  267.         /*Do it*/
  268.         if (selWinInfo)
  269.         {
  270.         ObjPtr object;
  271.         object = GetVar((ObjPtr) selWinInfo, REPOBJ);
  272.         if (object)
  273.         {
  274.             DeferMessage(object, repobjFunctions[whichFunction] . message);
  275.         }
  276.         }
  277.     }
  278.     }
  279.     return ObjTrue;
  280. }
  281.  
  282. static ObjPtr MakeRepobjActionActivated(action)
  283. ObjPtr action;
  284. /*Makes a repobj action activated*/
  285. {
  286.     ObjPtr allSelected;
  287.     ThingListPtr runner;
  288.     int f;
  289.     ObjPtr var;
  290.  
  291.     var = GetStringVar("MakeRepobjActionActivated", action, NAME);
  292.     if (!var) return ObjFalse;
  293.     f = NameToRepobjFunction(GetString(var));
  294.     if (f < 0) return ObjFalse;
  295.  
  296.     if (selWinInfo)
  297.     {
  298.     ObjPtr object;
  299.     object = GetVar((ObjPtr) selWinInfo, REPOBJ);
  300.  
  301.     if (object && GetMethod(object, repobjFunctions[f] . message))
  302.     {
  303.         SetVar(action, ACTIVATED, ObjTrue);
  304.         return ObjTrue;
  305.     }
  306.     }
  307.     SetVar(action, ACTIVATED, ObjFalse);
  308.     return ObjTrue;
  309. }
  310.  
  311. #ifdef PROTO
  312. void InitRepobjFunctions(void)
  313. #else
  314. void InitRepobjFunctions()
  315. #endif
  316. /*Initializes the repobj function system*/
  317. {
  318.     int k;
  319.     char bHelpString[200], mHelpString[200], scriptString[40];
  320.  
  321.     /*Make the class for repobj actions*/
  322.     repobjActionClass = NewObject(actionClass, 0L);
  323.     AddToReferenceList(repobjActionClass);
  324.     SetMethod(repobjActionClass, ACTIONMETHOD, RepobjAction);
  325.     SetVar(repobjActionClass, ACTIVATED, ObjFalse);
  326.     SetMethod(repobjActionClass, ACTIVATED, MakeRepobjActionActivated);
  327.     DeclareDependency(repobjActionClass, ACTIVATED, READYTOACTIVATE);
  328.  
  329.     /*Define the actions*/
  330.     DefineRepobjFunction(RF_SAVEPALETTE, SAVECPANEL, "save controls",
  331.     "Pressing this button saves the palette in this control panel to a file.",
  332.     "Choosing this menu item saves the palette in this control panel to a file.",
  333.     colorMenu, 1);
  334.  
  335.     DefineRepobjFunction(RF_KEEPPALETTE, KEEP, "keep",
  336.     "Pressing this button keeps the changes that you have made to the palette.  \
  337. Later, you can use Revert to Original to revert to this state.",
  338.     "Choosing this menu item keeps the changes that you have made to the palette.  \
  339. Later, you can use Revert to Original to revert to this state.",
  340.     colorMenu, 1);
  341.  
  342.     DefineRepobjFunction(RF_REVERTPALETTE, REVERT, "revert",
  343.     "Pressing this button reverts the palette in the conrtol panel to the state it had the last time \
  344. you chose Keep Changes, or to the original state if Keep Changes has not been chosen.",
  345.     "Choosing this menu item reverts the palette in the control panel to the state it had the last time \
  346. you chose Keep Changes, or to the original state if Keep Changes has not been chosen.",
  347.     colorMenu, 1);
  348.  
  349.     for (k = 0; k < NCOLORMODELS; ++k)
  350.     {
  351.     sprintf(scriptString, "model %s", colorModelNames[k]);
  352.     sprintf(bHelpString, 
  353.       "Pressing this button converts the palette in the conrtol panel to the %s color model.",
  354.       colorModelNames[k]);
  355.     sprintf(mHelpString, 
  356.       "Choosing this menu item converts the palette in the conrtol panel to the %s color model.",
  357.       colorModelNames[k]);
  358.  
  359.     DefineRepobjFunction(colorModelNames[k], colorModelMethods[k], 
  360.         scriptString, bHelpString, mHelpString, colorModelMenu, 1);
  361.     }
  362. }
  363.  
  364. #ifdef PROTO
  365. void KillRepobjFunctions(void)
  366. #else
  367. void KillRepobjFunctions()
  368. #endif
  369. /*Kills the repobj function system*/
  370. {
  371.     int k;
  372.  
  373.     for (k = 0; k < nRepobjFunctions; ++k)
  374.     {
  375.     SAFEFREE(repobjFunctions[k] . name);
  376.     SAFEFREE(repobjFunctions[k] . scriptLine);
  377.     SAFEFREE(repobjFunctions[k] . menuHelp);
  378.     SAFEFREE(repobjFunctions[k] . buttonHelp);
  379.     }
  380.     SAFEFREE(repobjFunctions);
  381.     RemoveFromReferenceList(repobjActionClass);
  382. }
  383.  
  384.